/* const nums = [2, 5, 34, 65, 21];

function createIterator(arr) {
  let i = 0;
  return {
    next() {
      return {
        value: arr[i++],
        done: i >= arr.length,
      };
    },
  };
}

const iterator = createIterator(nums);
let data = iterator.next();
while (!data.done) {
  console.log(data);
  data = iterator.next();
} */

// 依次得到斐波拉其数列前面n位的值
function getfeiboIterator() {
  let pre1 = 1,
    pre2 = 1,
    n = 1;
  return {
    next() {
      let value;
      if (n <= 2) {
        value = 1;
      } else {
        value = pre1 + pre2;
      }
      const result = {
        value,
        done: false,
      };

      pre2 = pre1;
      pre1 = result.value;
      n++;
      return result;
    },
  };
}

console.log(getfeiboIterator().next());
console.log(getfeiboIterator().next());
console.log(getfeiboIterator().next());
